package com.zyh.app.gradle.musicview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengyouhong on 2018/11/24.
 */
public class MusicSingStateView extends View {
    private static final String tag = "MusicSingStateView";
    private static final float MAX_ANGLE = 360 / 15;
    private static float MIN_ANGLE = 0.5f * 0.3f;
    private Context mContext;
    private int mColor;
    private Paint mPaint;

    private List<AngleState> angles = new ArrayList<>();
    private float rotate = 90f;


    public MusicSingStateView(Context context) {
        this(context, null);
    }

    public MusicSingStateView(Context context,
                              @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MusicSingStateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        mColor = Color.parseColor("#ffcc5c");
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(dp2px(3));
        mPaint.setColor(mColor);
        mPaint.setTextSize(dp2px(18));

        for (int i = 0; i < 20; i++) {
            float angle = MAX_ANGLE / 20 * i;
            AngleState angleState = new AngleState();
            angleState.angle = angle;
            angleState.controState = 1;
            angles.add(angleState);
        }
    }


    public boolean flag = true;

    public void setToggle(boolean toggle) {
        flag = toggle;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        canvas.rotate(rotate, getWidth() * 0.5f, getHeight() * 0.5f);
        RectF rect = new RectF(getLeft() + 20, getTop() + 20, getRight() - 20, getBottom() - 20);

        float startAngle = 0;
        float sweepAngle = 0;
        int per = 360 / 20;

        for (int i = 0; flag && i < 20; i++) {
            startAngle = per * i;
            sweepAngle = controlAngle(i);
            startAngle -= sweepAngle * 0.5f;
            canvas.drawArc(rect, startAngle, sweepAngle, false, mPaint);
        }

        postInvalidateDelayed(20);
        rotate -= 1.3f;
        canvas.restore();

    }

    private float controlAngle(int i) {
        AngleState angleState = angles.get(i);

        if (angleState.angle > MAX_ANGLE) {
            angleState.controState = 2;
        } else if (angleState.angle < MIN_ANGLE) {
            angleState.controState = 1;
        }

        if (angleState.controState == 1) {
            angleState.angle += 0.08f;
        } else {
            angleState.angle -= 0.08f;
        }

        return angleState.angle;

    }

    private float dp2px(float dpValue) {
        return getResources().getDisplayMetrics().density * dpValue;
    }

    class AngleState{
        float angle;
        int controState;
    }
}
